package com.project.easy.common.framework.web.validator.processor;



import com.project.easy.common.framework.web.annotation.ApiValidator;
import com.project.easy.common.framework.web.annotation.AuthValidator;
import com.project.easy.common.framework.web.annotation.ParamValidator;
import com.project.easy.common.framework.web.entity.dto.WebAuthFilterDto;
import com.project.easy.common.framework.web.servlet.HttpServletUtils;
import  com.project.easy.common.util.filter.WebAuthFilter;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.Map;
import java.util.Objects;


/**
 * @author GreatorMan.cool
 * @copyright: Copyright (c) 2020
 * @company: 扬起科技有限公司
 * @date 2021/10/12 21:02
 * @description:参数验证
 */
@Aspect
@Component
public class ValidatorProcessor implements Ordered {

    private static final org.slf4j.Logger LOG = LoggerFactory.getLogger(ValidatorProcessor.class);

    @Autowired(required = false)
    private Map<String, WebAuthFilter> webAuthFilterMap;
    /**
     * 验证拦截
     * @return  执行返回参数
     * @param jp 切面
     * @throws Throwable 异常
     */
    @Around("@annotation(com.project.easy.common.framework.web.annotation.ApiValidator) || "
            + "@annotation(com.project.easy.common.framework.web.annotation.AuthValidator) ||"
            + " @annotation(com.project.easy.common.framework.web.annotation.ParamValidator)")
    public Object doValidator(ProceedingJoinPoint jp) throws Throwable {
        Method method = ((MethodSignature) jp.getSignature()).getMethod();
        ApiValidator apiValidator = AnnotationUtils.findAnnotation(method, ApiValidator.class);
        AuthValidator authValidator = AnnotationUtils.findAnnotation(method, AuthValidator.class);
        ParamValidator paramValidator = AnnotationUtils.findAnnotation(method, ParamValidator.class);
        WebAuthFilterDto webAuthFilterDto = new WebAuthFilterDto(apiValidator, authValidator, paramValidator, jp);
        if (!Objects.isNull(webAuthFilterMap) && !webAuthFilterMap.isEmpty()) {
            webAuthFilterMap.forEach((serviceName, service) -> {
                service.doFilter(HttpServletUtils.getHttpServletRequest(), HttpServletUtils.getHttpServletResponse(), webAuthFilterDto);
                LOG.info("验证烂机器:{}执行完成", serviceName);
            });
        }
       return jp.proceed();
    }


    /**
     * Get the order value of this object.
     * <p>Higher values are interpreted as lower priority. As a consequence,
     * the object with the lowest value has the highest priority (somewhat
     * analogous to Servlet {@code load-on-startup} values).
     * <p>Same order values will result in arbitrary sort positions for the
     * affected objects.
     *
     * @return the order value
     * @see #HIGHEST_PRECEDENCE
     * @see #LOWEST_PRECEDENCE
     */
    @Override
    public int getOrder() {
        return LOWEST_PRECEDENCE;
    }
}
